﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using log4net.Core;
using log4net;
using SharpML.Gui.Extensions;
using SharpML.Gui.Properties;
using SharpML.Api.Implementation;

namespace SharpML.Gui.Forms
{
    public partial class Main : Form
    {
        #region - Private Static Member Variables -

            /// <summary>
            /// Simple logging pattern
            /// </summary>
            private static readonly ILog Log = LogManager.GetLogger( System.Reflection.MethodBase.GetCurrentMethod().DeclaringType );

        #endregion

        #region - Construction -

            public Main()
            {
                InitializeComponent();

                SetStyle( ControlStyles.UserPaint, true );
                SetStyle( ControlStyles.AllPaintingInWmPaint, true );
                SetStyle( ControlStyles.DoubleBuffer, true );

                masterView.ShowLog += ShowLog;
                masterView.HideLog += HideLog;

                if( !Properties.Settings.Default.ShowLog )
                    HideLog();
            }

        #endregion

        #region - Private Functionality -

            public void ShowLog()
            {
                if( !Controls.Contains( masterView ) )
                    return;

                Controls.Remove( masterView );
                splitContainer.Show();

                splitContainer.Panel1.Controls.Add( masterView );
            }

            public void HideLog()
            {
                Properties.Settings.Default.Save();

                if( !splitContainer.Panel1.Controls.Contains( masterView ) )
                    return;

                splitContainer.Panel1.Controls.Remove( masterView );
                splitContainer.Hide();

                Controls.Add( masterView );
            }

        #endregion

        #region - Form Event Handlers -

            private void Main_Load( object sender, EventArgs e )
            {
                // Hookup log
                SimpleLoggingPublisher.Activity += SimpleLoggingPublisher_Activity;
            }

            void SimpleLoggingPublisher_Activity( log4net.Core.Level level, string renderedMessage )
            {
                // UI Delegate, this is fired off-thread
                this.AsyncGuiOp( (Action)delegate
                {

                    // Locals
                    Color color;

                    // Choose fancy color, most likely to least likely
                    if( level <= Level.Info )
                        color = SystemColors.WindowText;
                    else if( level < Level.Error )
                        color = Color.Blue;
                    else
                        color = Color.Red;

                    // Append log text
                    richTextBoxLog.AppendText( renderedMessage + "\r\n", color );

                    // Scroll into view
                    richTextBoxLog.SelectionStart = richTextBoxLog.Text.Length;
                    richTextBoxLog.ScrollToCaret();
                } );
            }

        #endregion
    }
}
